<head>
<title>Evoscan 2 Logworks</title>
<HTA:APPLICATION 
     APPLICATIONNAME="Operating System Version"
     SCROLL="yes"
     SINGLEINSTANCE="yes">
</head>

<script language="VBScript">

Option Explicit

dim colors
colors=Array(10053171,3355545,6710835,13369395,255,10027161,3368550,16711680,153,10027008,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)


function chooseFileToConvert
' evoscan2logworks
' copyright 2012 Antoni Koziol
'
'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'   GNU General Public License for more details.
'
'   You should have received a copy of the GNU General Public License
'   along with this program.  If not, see <http://www.gnu.org/licenses/>.
'
'
' This is currenty BETA, as such, no warranty is offered.
' Most of the code is hacked to get working quickly :)





'===========================
'== User changable values ==
'===========================

'time between each log entry in the logworks file
dim ENTRY_RESOLUTION
ENTRY_RESOLUTION=.082

Dim adoCSVConnection, adoCSVRecordSet, strPathToTextfile
Dim k
dim BOT,DIR_TYPE,NUM_TYPE,VALID,SEP,VECTORS_STR,TUPLES_STR,DATA_STR,TABLE_STR,IGNORE_STR,BOT_LINE_STR,STR_TYPE_LINE,EOD
Dim DBL_QT,SIN_QT,NL

'SIENNA	#A0522D	#996633
'DARKSLATEBLUE	#483D8B	#333399
'DARKOLIVEGREEN	#556B2F	#666633
'CRIMSON	#DC143C	#CC0033
'BLUE	#0000FF	#0000FF
'DARKMAGENTA	#8B008B	#990099
'DARKSLATEGRAY	#2F4F4F	#336666
'RED	#FF0000	#FF0000
'NAVY	#000080	#000099
'DARKRED	#8B0000	#990000
'BLACK	#000000	#000000


BOT="BOT"
DIR_TYPE="-1"
NUM_TYPE="0"
STR_TYPE_LINE="1,0"
VALID="V"
SEP=","
DATA_STR="DATA"
TUPLES_STR="TUPLES"
VECTORS_STR="VECTORS"
TABLE_STR="TABLE"
NL=vbCr & vbLf
BOT_LINE_STR=DIR_TYPE & SEP & NUM_TYPE & NL &BOT
IGNORE_STR="LogID,LogEntryDate,LogEntryTime,LogEntrySeconds"
DBL_QT=""""""
SIN_QT=""""
EOD="EOD"

'Put up some warranty information...
dim warranty
warranty=msgbox("This script to convert Evoscan logs to Logworks comes with no warranty, choose OK to continue",1)
if warranty<>vbcancel then   
   



'==========================================
' Preset file chooser dialog
Dim  fileBrowserFSO,dialogFSO


'need to rename the file because the ADODB connection won't handle it later on
dim inputtemp,objFSOb,sourceFile,infilename,outfilepath,extension

sourceFile= ChooseFile()
if sourcefile <> "" then


Set objFSOb = CreateObject("Scripting.FileSystemObject")

dim tempFolder,tempFilename
tempfolder= CreateObject("Scripting.FileSystemObject").GetSpecialFolder(2)

tempFileName="evoscan2logtemp.csv"
'inputTemp="C:\temp\evoscan2log temp.csv"
'tempFolder="C:\temp\"
Const OverwriteExisting = True
'copy the file to a temp
set sourceFile=objFSOb.GetFile(sourcefile)
strPathToTextFile=objFSOb.getparentfoldername(sourceFile)
'inputTemp= tempFolder&"\" & "evoscan2logtemp.csv"
inputTemp= strPathToTextfile &"\" & "evoscan2logtemp.csv"
'inputTemp="C:\evoscan2logtemp.csv"
objFSOb.CopyFile sourceFile, inputTemp, OverwriteExisting
'msgbox (objfsob.GetFileName(sourceFile))
'msgbox (objfsob.Getparentfoldername(sourceFile))
'save our source filename
infilename = objfsob.GetFileName(sourceFile)

extension = objfsob.GetExtensionName(sourceFile)
if extension<>"csv" then
   msgbox "'" & extension & "' is not a valid file type.  The source should be a csv file."
   wscript.quitW
end if

' Specify path to CSV file.
'strPathToTextFile = "c:\Scripts\"
'strPathToTextFile = "C:\Documents and Settings\Administrator\My Documents\Dropbox\carstuff\Scripts\"
'strPathToTextFile = "C:\Users\Antoni\Dropbox\carstuff\Scripts\"
'get our source file's path
strPathToTextFile=objFSOb.getparentfoldername(sourceFile)&"\"
outfilepath=strPathToTextfile  & objfsob.GetbaseName(sourceFile)&"_lw.dif"

' Open connection to the CSV file.
Set adoCSVConnection = CreateObject("ADODB.Connection")
Set adoCSVRecordSet = CreateObject("ADODB.Recordset")

' Open CSV file with header line.MSDASQL.1
'adoCSVConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
'adoCSVConnection.Open "Provider=MSDASQL.1;" & _
'    "Data Source=" & strPathtoTextFile & ";" & _
'    "Extended Properties=""text;HDR=YES;FMT=Delimited"""
'    inputTemp="C:\temp\evoscan2logtemp.csv"

'adoCSVConnection.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
'    "Data Source=[" & strPathToTextFile & "\];" & _
'    "Extended Properties=""text;HDR=YES;FMT=Delimited"""
'    inputTemp="C:\temp\evoscan2logtemp.csv"


'Provider=MSDASQL; Driver={Microsoft Text Driver (*.txt; *.csv)}; DBQ=C:\path\;
dim connectionString 
connectionString= "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" & strPathToTextfile & "\;"
adoCSVConnection.open connectionString

'msgbox sourceFile&NL&strPathToTextfile&NL&inputTemp&NL&tempFolder
adoCSVRecordset.Open "SELECT * FROM [" & tempFileName& "]", adoCSVConnection


confirmation=msgbox("Please verify the source and destination" & NL&NL & "Source: " &objfsob.getabsolutepathname(sourceFile)&NL&"Destination: "& outfilepath ,1)
if confirmation<>vbcancel then   

Dim objFSO,objTextFile,confirmation
Set objFSO = CreateObject("Scripting.FileSystemObject")
'msgbox outfilepath
Set objTextFile = objFSO.CreateTextFile(outfilepath, True)
 
if adoCSVRecordset.EOF=false then
   
   Dim vectors,tuples,setTuples,fieldCount,j,l,m

   'Stores all the data we want to know for the header info later on
   Dim fields()
   'fields(0) = name
   'fields(1) = min
   'fields(2) = max
   'fields(3) =   
   'fields(4) =
   'fields(5) =
   vectors=1
   tuples=0
   'Used through only one row of data to find out which columns to keep
   setTuples=true
   fieldCount=0

   dim ans1,arraydata,secs
   'ans1 is for debugging output panels
   ans1=vbok
   'accumlate time for each output data in logworks
   secs=0

   ' Read the CSV rows.
   Do Until adoCSVRecordset.EOF
       tuples=0
       ' Display all fields by name.
       objTextFile.Writeline(BOT_LINE_STR)       
       objTextFile.Writeline(NUM_TYPE & SEP & secs)
       objTextFile.Writeline(VALID)
       'accumlate the next time entry
       secs=secs + ENTRY_RESOLUTION
       'loop through the columns
       for k = 0 To adoCSVRecordset.Fields.Count - 1
         'only take columns that are not empty, for now it requires all rows to at least have some data
         'So if the first row is full and the second row has a blank, it will break, fun.
          If adoCSVRecordset.Fields(k).Value <> "" and InStr(IGNORE_STR,adoCSVRecordset.Fields(k).Name)=0 then
            'resize the field array only if we haven't done a row yet
             If setTuples Then
                ReDim preserve fields(3,tuples)
                fields(0,tuples)=adoCSVRecordset.Fields(k).name                               
             End If

               'set our mininum for the column, yes this is horrid
             if fields(1,tuples)="" or adoCSVRecordset.Fields(k).Value < fields(1,tuples) Then
                'if ans1=vbok then
                  'ans1=msgbox(adoCSVRecordset.Fields(k).Value &"<"& fields(1,tuples),1)
                'end if
                fields(1,tuples)=adoCSVRecordset.Fields(k).Value
             End If
            
               'set our maximum for the column, yes this is horrid
             if fields(2,tuples)="" or adoCSVRecordset.Fields(k).Value > fields(2,tuples) Then
                'if ans1=vbok then
                  'ans1=msgbox(adoCSVRecordset.Fields(k).Value &">"& fields(2,tuples),1)
                'end if
                fields(2,tuples)=adoCSVRecordset.Fields(k).Value
             End If
            'reset arraydata, this is to print summary info
            'arraydata=""              
'           for j=0 to ubound(fields,1)
'              for l=0 to ubound(fields,2)
'                    arraydata= arraydata& j & "," & l &"=" & fields(j,l) &NL
'              next
'           next

            'write out the actual data
            objTextFile.Writeline(NUM_TYPE & SEP & adoCSVRecordset.Fields(k).Value)
            objTextFile.Writeline(VALID)
            'increment our row
            tuples=tuples+1
          end if          
       Next 'end loop of columns
       setTuples=false
       'increment the lines
       vectors = vectors+1
       adoCSVRecordset.MoveNext
   Loop 'end loop of rows
end if

'print the summary of the field data
'if ans1=vbok then
'  ans1=msgbox(arraydata,1)
'end if

' Clean up.
objTextFile.Close

'reopen the file to append header information
Set objTextFile = objFSO.OpenTextFile(outfilepath,1 )
Dim ReadAllTextFile
'store the previously written data that has no dif header info
ReadAllTextFile = objTextFile.ReadAll
objTextFile.Close
'reopen destination file for writing
Set objTextFile = objFSO.OpenTextFile(outfilepath,2,True)

'let's write some dif header info
objTextFile.Writeline(TABLE_STR)
objTextFile.Writeline("0,1")
objTextFile.Writeline("""EXCEL""")
objTextFile.Writeline(VECTORS_STR)
objTextFile.Writeline("0," & vectors+12)
objTextFile.Writeline(DBL_QT)
objTextFile.Writeline(TUPLES_STR)
objTextFile.Writeline("0," & tuples+1)
objTextFile.Writeline(DBL_QT)
objTextFile.Writeline(DATA_STR)
objTextFile.Writeline("0,0")
objTextFile.Writeline(DBL_QT)

'Write logworks header info
objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Input Description""")
For k=1 to tuples
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(DBL_QT)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Stochiometric:""")
For k=1 to tuples
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(DBL_QT)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """From Device:""")
For k=1 to tuples
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline("""(EVOSCAN"&k &")""")
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Name:""")
For k=0 to ubound(fields,2)
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(SIN_QT & fields(0,k)&SIN_QT )
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Unit:""")
For k=0 to ubound(fields,2)
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(SIN_QT & left(fields(0,k),3)&SIN_QT )
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Range:""")
For k=0 to ubound(fields,2)   
   if fields(1,k) > 2 then
      fields(1,k)=fields(1,k)-2
   end if
   objTextFile.Writeline("0," & fix(fields(1,k)) )
   objTextFile.Writeline(VALID)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """equiv(Sample):""")
For k=1 to tuples
   objTextFile.Writeline("0,0")
   objTextFile.Writeline(VALID)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """to:""")
For k=0 to ubound(fields,2)   
   objTextFile.Writeline("0," & fix(fields(2,k))+1 )
   objTextFile.Writeline(VALID)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """equiv(Sample):""")
For k=1 to tuples
   objTextFile.Writeline("0,4096")
   objTextFile.Writeline(VALID)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Color:""")
For k=0 to tuples-1
   objTextFile.Writeline("0,"& getFieldColor("test",k))
   objTextFile.Writeline(VALID)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """-End-""")
For k=1 to tuples
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(DBL_QT)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Session 1""")
For k=1 to tuples
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(DBL_QT)
Next

objTextFile.Writeline(BOT_LINE_STR)
objTextFile.Writeline(STR_TYPE_LINE & NL & """Time(sec)""")
For k=0 to tuples-1
   objTextFile.Writeline(STR_TYPE_LINE)
   objTextFile.Writeline(SIN_QT & fields(0,k) & " (" & left(fields(0,k),3) & ")" & SIN_QT )
Next


'write out data we saved from before
'should consider a better way, won't scale with large files
objTextFile.Write(ReadAllTextFile)
objTextFile.Writeline("-1,0")
objTextFile.Writeline(EOD)

objTextFile.Close

'copy to a text file for easier debugging
'objFSO.CopyFile strPathToTextfile & "out.txt", strPathToTextfile & "out.dif"

adoCSVRecordset.Close
adoCSVConnection.Close
msgbox "Conversion completed successfully"
end if
else
        msgbox "You must choose an Evoscan file to convert..."
end if
end if
'subroutine to choose a color


end function

function getFieldColor(fieldName,fieldIdx)
   getFieldColor=colors(fieldIdx)
end function

 Function ChooseFile()
     Dim Result
     Dim IE : Set IE = CreateObject("InternetExplorer.Application")
     With IE
         .Visible = true
         .Navigate("about:blank")
         Do Until .ReadyState = 4 : Loop
         With .Document
             .Write "<html><body><input id='f' type='file'></body></html>"
             With .All.f
                 .Focus
                 .Click
                 Result = .Value
             End With
         End With
         .Quit
     End With
     Set IE = Nothing
    ChooseFile = Result
 End Function
 
 

</script>

<body>
<input type="button" value="Choose file to convert" name="run_button"  onClick="chooseFileToConvert">

</body>


